package Kalah;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class NewOptionsMenu extends JDialog implements ActionListener {
	private static NewOptionsMenu m_ref;
	
	private JButton m_ok;
	private JPanel m_jp;

	private JRadioButton m_humanButtonTop;
	private JRadioButton m_humanButtonBot;
	private JRadioButton m_compButtonTop;
	private JRadioButton m_compButtonBot;

	private boolean m_isTopHuman;
	private boolean m_isBotHuman;
	private boolean m_isClosed;
	private boolean m_gameCreated;
	public boolean getTopIsHuman() {
		return m_isTopHuman;
	}

	public boolean getBotIsHuman() {
		return m_isBotHuman;
	}

	private void buildUI() {
		JPanel row1 = new JPanel(new BorderLayout());
		JPanel row2 = new JPanel(new BorderLayout());
		JLabel topLabel = new JLabel("Top Player   :", SwingConstants.RIGHT);
		JLabel bottomLabel = new JLabel("Bottom Player:",
				SwingConstants.RIGHT);
		row1.add(topLabel, BorderLayout.WEST);
		row2.add(bottomLabel, BorderLayout.WEST);
		ButtonGroup topGroup = new ButtonGroup();
		ButtonGroup botGroup = new ButtonGroup();
		m_humanButtonTop = new JRadioButton("Human");
		m_humanButtonBot = new JRadioButton("Human");
		m_humanButtonTop.setSelected(true);
		m_humanButtonBot.setSelected(true);
		m_compButtonTop = new JRadioButton("Computer");
		m_compButtonBot = new JRadioButton("Computer");
		topGroup.add(m_humanButtonTop);
		topGroup.add(m_compButtonTop);
		botGroup.add(m_humanButtonBot);
		botGroup.add(m_compButtonBot);
		row1.add(m_humanButtonTop, BorderLayout.CENTER);
		row1.add(m_compButtonTop, BorderLayout.EAST);
		row2.add(m_humanButtonBot, BorderLayout.CENTER);
		row2.add(m_compButtonBot, BorderLayout.EAST);
		m_jp.add(row1);
		m_jp.add(row2);
		m_ok = new JButton("Create New Game");
		m_ok.addActionListener(this);
		m_jp.add(m_ok);
	}

	private NewOptionsMenu(JFrame parent) {
		super(parent, "New Game Options", true);

		m_jp = new JPanel(new GridLayout(0, 1, 5, 5));
		m_jp.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
		add(m_jp);
		buildUI();
		pack();
		setLocationRelativeTo(parent);
		setVisible(true);

	}
	public static NewOptionsMenu getInstance(){
		if(m_ref == null)
			m_ref = new NewOptionsMenu(KalahFrame.getInstance());
		return m_ref;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		
		m_isBotHuman = m_humanButtonBot.isSelected() ? true : false;
		m_isTopHuman = m_humanButtonTop.isSelected() ? true : false;
		m_isClosed = true;
		m_gameCreated = true;
		setVisible(false);

	}
}